<?php


namespace App\Http\Controllers;

use App\Models\Game;
use App\Models\Score;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class ScoresController extends BaseController
{
    public function gameScores($slug){
        $game_id = Game::where('slug', $slug)->value('id');
        $users = User::select('id', 'username')->get();

        $data = [];
        foreach ($users as $user){
            $scores = Score::where('game_id', $game_id)
                ->where('user_id', $user->id)
                ->orderby('score', 'desc')
                ->select('score', 'created_at')
                ->first();

            if ($scores != null){
                $scores['username'] = $user->username;
                $scores['uploadTimestamp'] = $scores['created_at'];
                $data[] = $scores;
            }
        }

        $column = array_column($data,'score');
        array_multisort($column,SORT_DESC,$data);

        return $this->returnJson([
            'score' => $data
        ]);
    }

    public function addScore(Request $request, $slug){
        $only = $request->only(['score']);

        $validator = Validator::make($only, [
            'score' => 'required|numeric',
        ], []);

        if ($validator->fails()){
            return $this->returnInvalid($validator->errors()->getMessages());
        }

        $game = Game::where('slug', $slug)->select(['id', 'scoreCount'])->first();

        if ($game == null){
            return $this->returnInvalid('slug not find');
        }

        Score::insert([
            'user_id' => $request->user->user->id,
            'game_id' => $game->id,
            'score' => $only['score'],
            'created_at' => time()
        ]);

        Game::where('slug', $slug)->update([
            'scoreCount' => $game->scoreCount? $game->scoreCount + 1 : 1
        ]);

        return $this->returnJson([], self::NEW_RESOURCES);
    }

    //D 列出每场游戏的最高分数
    public function getTopScore(Request $request){
        $user_id = $request->user->user->id;

        $games = Game::orderby('title', 'asc')
            ->get();

        $scores = [];
        foreach ($games as $game){
            $score = Score::where('user_id', $user_id)
                ->where('game_id', $game->id)
                ->orderby('score', 'desc')
                ->first();

            if($score != null){
                $score['game'] = $game;
                $scores[] = $score;
            }
        }
        return $this->returnJson([
            'data' => $scores
        ]);
    }

    //D 当前用户游戏最高分数
    public function getUserGameTop(Request $request, $slug){
        $user_id = $request->user->user->id;

        $score = Score::where('game_id', Game::where('slug', $slug)->value('id'))
            ->where('user_id', $user_id)
            ->orderby('score', 'desc')
            ->first();

        $data['username'] = $request->user->user->username;
        //定义未游玩时分数字样
        $data['score'] = $score->score ?? '未游戏';
        return $this->returnJson($data);
    }
}
